<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-23
 * Time: 01:15
 * 合伙人
 */

namespace app\api\controller\v1;


use app\api\model\Assistant;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\MemberCost;
use app\api\model\MemberOrder;
use app\api\model\ProvincePartner;
use app\api\model\User;
use app\api\model\UserInvitation;
use app\api\model\UserInvitationMoney;
use think\facade\Request;

class Partner
{
    /**
     * 获取省份合伙人
     * @validate('PagingParameter')
     * @auth('省份合伙人','合伙人管理')
     */
    public function getProvinceList($page,$limit,$province)
    {
        $where = [];
        if($province) {
            $where[] = ['province','like','%'.$province.'%'];
        }
        $query = ProvincePartner::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '申请中';
            }elseif($item['status'] == 2){
                $item['status_name'] = '已通过';
            } else {
                $item['status_name'] = '未通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getProvinceInfo($id)
    {
        return ProvincePartner::where('id',$id)->find();
    }

    /**
     * 审核通过并修改信息
     */
    public function updateProvince()
    {
        $data = Request::put();
        $info = ProvincePartner::get($data['id']);
        if($data['status'] == 2) {
            if($data['status'] != $info['status']) {
                if($info['status_num'] == 1) {
                    $this->changeUserMoney($info,'province_discount',3);
                }
            }
        }
        $data['status_num'] = $info['status_num'] + 1;
        $ProvincePartner = new ProvincePartner();
        $ProvincePartner->allowField(true)->save($data,['id'=>$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 省份代理升级返佣操作
     */
    public function changeUserMoney($info,$ziduan,$status)
    {
        $userInvite = UserInvitation::where('invited_person_id',$info['user_id'])->find();
        if($userInvite) {
           $member_order = MemberOrder::where('user_id',$info['user_id'])->find();
           if($member_order) {
                $member_cost = MemberCost::where('id',$member_order['member_cost_id'])->find();
                if($member_cost) {
                   if($member_cost[$ziduan] > 0) {
                       $userInfo = User::get($userInvite['inviter_id']);
                       UserInvitationMoney::create([
                           'user_id' => $userInfo['id'],
                           'invite_id' => $info['user_id'],
                           'type' => $status,
                           'money' => $member_cost[$ziduan],
                           'user_invite_money' => $userInfo['invite_money'],
                           'order_no' => '',
                       ]);
                       User::where('id',$userInfo['id'])->update([
                           'money' => round($userInfo['money']+$member_cost[$ziduan])
                       ]);
                   }
                }
           }
        }
    }

    /**
     * 删除信息
     */
    public function deleteProvince($id)
    {
        $info = ProvincePartner::get($id);
        ProvincePartner::destroy($id);
        User::where('id',$info['user_id'])->update([
            'partner_status' => 0
        ]);
        return writeJson(203, [], '删除成功！');
    }

    /**
     * 获取市合伙人
     * @validate('PagingParameter')
     * @auth('市级合伙人','合伙人管理')
     */
    public function getCityList($page,$limit,$province,$city)
    {
        $where = [];
        if($province) {
            $where[] = ['province','like','%'.$province.'%'];
        }
        if($city) {
            $where[] = ['city','like','%'.$city.'%'];
        }
        $query = CityPartner::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '申请中';
            }elseif($item['status'] == 2){
                $item['status_name'] = '已通过';
            } else {
                $item['status_name'] = '未通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getCityInfo($id)
    {
        return CityPartner::where('id',$id)->find();
    }

    /**
     * 审核通过并修改信息
     */
    public function updateCity()
    {
        $data = Request::put();
        $info = CityPartner::get($data['id']);
        if($data['status'] == 2) {
            if($data['status'] != $info['status']) {
                if($info['status_num'] == 1) {
                    $this->changeUserMoney($info,'city_discount',4);
                }
            }
        }
        $CityPartner = new CityPartner();
        $CityPartner->allowField(true)->save($data,['id'=>$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除信息
     */
    public function deleteCity($id)
    {
        $info = CityPartner::get($id);
        CityPartner::destroy($id);
        User::where('id',$info['user_id'])->update([
            'partner_status' => 0
        ]);
        return writeJson(203, [], '删除成功！');
    }

    /**
     * 获取区/县合伙人
     * @validate('PagingParameter')
     * @auth('区/县合伙人','合伙人管理')
     */
    public function getCountyList($page,$limit,$province,$city,$county)
    {
        $where = [];
        if($province) {
            $where[] = ['province','like','%'.$province.'%'];
        }
        if($city) {
            $where[] = ['city','like','%'.$city.'%'];
        }
        if($county) {
            $where[] = ['county','like','%'.$county.'%'];
        }
        $query = CountyPartner::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '申请中';
            } elseif($item['status'] == 2){
                $item['status_name'] = '已通过';
            }  elseif($item['status'] == 3) {
                $item['status_name'] = '未通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getCountyInfo($id)
    {
        return CountyPartner::where('id',$id)->find();
    }

    /**
     * 审核通过并修改信息
     */
    public function updateCounty()
    {
        $data = Request::put();
        $info = CountyPartner::get($data['id']);
        if($data['status'] == 2) {
            if($data['status'] != $info['status']) {
                if($info['status_num'] == 1) {
                    $this->changeUserMoney($info,'county_discount',4);
                }
            }
        }
        $CountyPartner = new CountyPartner();
        $CountyPartner->allowField(true)->save($data,['id'=>$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除信息
     */
    public function deleteCounty($id)
    {
        $info = CountyPartner::get($id);
        CountyPartner::destroy($id);
        User::where('id',$info['user_id'])->update([
            'partner_status' => 0
        ]);
        return writeJson(203, [], '删除成功！');
    }

    /**
     * 助理列表
     * @validate('PagingParameter')
     * @auth('助理','合伙人管理')
     */
    public function getAssistantList($type,$page,$limit)
    {
        $where = [];
        if($type) {
            $where[] = ['type','eq',$type];
        }
        $query = Assistant::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['type'] == 1) {
                $item['type_name'] = '省级代理';
                $item['partner'] = ProvincePartner::get($item['pid']);
            } elseif($item['type'] == 2){
                $item['type_name'] = '市级代理';
                $item['partner'] = CityPartner::get($item['pid']);
            }  elseif($item['type'] == 3) {
                $item['type_name'] = '区/县代理';
                $item['partner'] = CountyPartner::get($item['pid']);
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getAssistantInfo($id)
    {
        return Assistant::where('id',$id)->find();
    }

    /**
     * 审核通过并修改信息
     */
    public function updateAssistant()
    {
        $data = Request::put();
        $Assistant = new Assistant();
        $Assistant->allowField(true)->save($data,['id'=>$data['id']]);
        return writeJson(202, [], '修改成功！');
    }


    /**
     * 删除信息
     */
    public function deleteAssistant($id)
    {
        $info = Assistant::get($id);
        Assistant::destroy($id);
        User::where('id',$info['user_id'])->update([
            'partner_status' => 0
        ]);
        return writeJson(203, [], '删除成功！');
    }
}